package zsd.reiview.utility;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.nodes.TagNode;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.HtmlPage;




/**
 * @ClassName: ParserUtility
 * @Description:
 * @author 朱圣代
 * @date 2011-8-2 下午03:45:45
 */

public class ParserUtility extends Utility {

	  
	 /** 
		* @Title: TagStartFilter 
		* @Description: 得到一个自定义的过滤器
		* @param @param start
		* @param @return
		* @return NodeFilter
		* @throws 
		* @date 2011-8-17 下午08:42:41 
		*/  
	    
	public static NodeFilter TagStartFilter(final String start) {
		NodeFilter tagFilter = new NodeFilter() {

			@Override
			public boolean accept(Node node) {

				if ((node instanceof TagNode) && (node.getText().indexOf(start) == 0))
					return true;
				return false;

			}
		};
		return tagFilter;
	}

	/**
	 * @Title: getParser
	 * @Description:得到一个Parser
	 * @param @param str
	 * @param @return
	 * @return Parser
	 * @throws
	 * @date 2011-8-2 下午03:45:40
	 */

	public static Parser getParser(String str) {
		Parser parser = null;
		try {
			parser = new Parser();
			parser.setInputHTML(str);
			parser.setEncoding(parser.getEncoding());
		} catch (ParserException e) {
			e.printStackTrace();
		}
		return parser;
	}

	public static String TagName(String str) {
		int pos;
		if ((pos = str.indexOf(" ")) != -1)
			return str.substring(0, pos);
		return str;
	}

	/**
	 * @Title: isNeedTag
	 * @Description:过滤不想要的标签
	 * @param @param str
	 * @param @return
	 * @return boolean
	 * @throws
	 * @date 2011-8-2 下午03:45:34
	 */

	public static boolean isNeedTag(String str) {

		if (str.startsWith("/"))
			return false;
		if (str.startsWith("script"))
			return false;
		if (str.startsWith("noscript"))
			return false;
		if (str.startsWith("span"))
			return false;
		if (str.startsWith("a"))
			return false;
		if (str.startsWith("img"))
			return false;
		if (str.startsWith("ul"))
			return false;
		if (str.startsWith("li"))
			return false;
		if (str.startsWith("h1"))
			return false;
		if (str.startsWith("br"))
			return false;
		if (str.startsWith("p"))
			return false;
		/*
		 * if( !str.startsWith("div")) return false;
		 */
		return true;
	}

	  
	 /** 
		* @Title: getNode 
		* @Description:得到以标签以str开头的节点
		* @param @param parser
		* @param @param str
		* @param @return
		* @return Node
		* @throws 
		* @date 2011-8-17 下午08:43:42 
		*/  
	    
	public static Node getNode(Parser parser, final String str) {
		NodeList list1;
		try {
			list1 = parser.extractAllNodesThatMatch(ParserUtility.TagStartFilter(str));
			if (list1 == null || list1.size() < 1)
				return null;
			return list1.elementAt(0);
		} catch (ParserException e) {

			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return null;
	}

	  
	 /** 
		* @Title: getNodeList 
		* @Description:得到以标签以str开头的节点
		* @param @param parser
		* @param @param str
		* @param @return
		* @return NodeList
		* @throws 
		* @date 2011-8-17 下午08:44:42 
		*/  
	    
	public static NodeList getNodeList(Parser parser, final String str) {
		NodeList list1;
		try {
			list1 = parser.extractAllNodesThatMatch(ParserUtility.TagStartFilter(str));
			if (list1 == null || list1.size() < 1)
				return null;
			return list1;
		} catch (ParserException e) {

			// TODO Auto-generated catch block
			e.printStackTrace();

		}
		return null;
	}

	public static List nodeListToList(NodeList nodeList) {
		if (nodeList == null)
			return null;
		List list = new ArrayList();
		// list.addAll(nodeList);
		for (int i = 0; i < nodeList.size(); i++) {
			list.add(nodeList.elementAt(i));
		}
		return list;
	}

	  
	 /** 
		* @Title: getTitle 
		* @Description: 得到网页的题目
		* @param @param str
		* @param @return
		* @return String
		* @throws 
		* @date 2011-8-17 下午08:45:04 
		*/  
	    
	public static String getTitle(String str) {
		Parser parser = getParser(str);
		HtmlPage page = new HtmlPage(parser);

		try {
			parser.visitAllNodesWith(page);
			return page.getTitle();
		} catch (ParserException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {

		// TODO Auto-generated method stub

	}

}
